package mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.qslll.library.fragments.ExpandingFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.BaseApplication;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.contact.InfoContact;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.ActivityUtils;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.ToastUtlis;
import mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF.BaseFragmentActivity;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.component.DaggerInfoAddComponent;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.modules.InfoAddModules;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment.ArticeFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment.BezierFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment.HomePageFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment.MyFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment.NewsSportFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.presenter.InfoAddPresenter;

/**
 * Created by ${cbf} on 2017/11/24.
 */

public class StartActivity extends BaseFragmentActivity implements BottomNavigationBar.OnTabSelectedListener, InfoContact.InfoAddView,ExpandingFragment.OnExpandingClickListener {
    @BindView(R.id.first_activity_navigationbar)
    BottomNavigationBar mBottomNavigationBar;
    @BindView(R.id.first_activity_framlayout)
    FrameLayout framlayout;
    @BindView(R.id.title)
    LinearLayout lltitle;
    @BindView(R.id.back_off)
    LinearLayout backoff;
    private FragmentManager fm;
    private long exitTime;
    private List<Fragment> fragments;
    private Fragment showFragment;
    private InfoAddPresenter presenter;

    @Override
    protected int setContentLayout() {
        return R.layout.activity_start;
    }

    @Override
    protected void init() {
        initTitleBar(lltitle, "首页", true, new String[]{"添加", "搜索"});
        backoff.setVisibility(View.GONE);
        fragments = new ArrayList<>();
        fragments.add(HomePageFragment.Instance(this, "home"));
        fragments.add(NewsSportFragment.Instance(this, "mall"));
//        fragments.add(BezierFragment.Instance(this, "mallInfo"));
        fragments.add(ArticeFragment.Instance(this, "mallInfo"));
        fragments.add(MyFragment.Instance(this, "my"));
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottomNavigationBar.setInActiveColor(R.color.black);
        fm = getSupportFragmentManager();
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.home, "首页")).setActiveColor(R.color.bottomcolor)
                .addItem(new BottomNavigationItem(R.drawable.qian, "商城")).setActiveColor(R.color.bottomcolor)
                .addItem(new BottomNavigationItem(R.drawable.message, "商城质询")).setActiveColor(R.color.bottomcolor)
                .addItem(new BottomNavigationItem(R.drawable.personal, "个人")).setActiveColor(R.color.bottomcolor)
                .setFirstSelectedPosition(0)
                .initialise();
        mBottomNavigationBar.selectTab(0);
        presenter = DaggerInfoAddComponent.builder()
                .infoAddModules(new InfoAddModules(this))
                .build()
                .getPresenter();
        presenter.infoAddPresenter("20", "1111111111");

    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void setListeners() {
        mBottomNavigationBar.setTabSelectedListener(this);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onTabSelected(int position) {
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        if (showFragment == null) {
            transaction.add(R.id.first_activity_framlayout, fragments.get(position)).commit();
            showFragment = fragments.get(position);
            return;
        }
        if (!showFragment.getArguments().getString("name").equals(fragments.get(position).getArguments().getString("name"))) {
            if (fragments.get(position).isAdded()) {
                transaction.hide(showFragment).show(fragments.get(position)).commit();
                showFragment = fragments.get(position);
            } else {
                transaction.hide(showFragment).add(R.id.first_activity_framlayout, fragments.get(position)).commit();
                showFragment = fragments.get(position);
            }
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (System.currentTimeMillis() - exitTime < 2000) {
                ActivityUtils.removeAll();
            } else {
                exitTime = System.currentTimeMillis();
                ToastUtlis.ToastShow_Short(BaseApplication.getContext(), "再点击一次退出");
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void infoAddView(String s) {

    }

    @Override
    public void onExpandingClick(View v) {

    }
}
